import { queryNotices } from '@/services/user';
import router from 'umi/router'

const GlobalModel = {
  namespace: 'global',
  state: {
    collapsed: true,
    notices: [],
    menus: [],
    btns: [],
    hasSearchBtn: false,
    supplierType: 2,      // 2: 商户 ， 4：门店
    // 订单平台来源
    platForms: [
      {
          label: "POS机",
          value: 'POS'
      },
      {
          label: "APP",
          value: 'APP'
      },
      {
          label: "PC",
          value: 'PC'
      },
      {
          label: "微信小程序",
          value: 'WECHAT_MICROSOFT'
      },
      {
          label: "微信公众号",
          value: 'WECHAT_PUB'
      },
      {
          label: "触摸大屏",
          value: 'TOUCH_SCREEN'
      },
      {
          label: "支付宝小程序",
          value: 'ZFB_MICROSOFT'
      },
      {
          label: "支付宝生活号",
          value: 'ZFB_PUB'
      },
      {
          label: "外部API",
          value: 'EXTERNAL_API'
      },
    ],
    // 支付方式
    payTypes: [
      {
          label: "微信支付",
          value: 'WXP'
      },
      {
          label: "支付宝支付",
          value: 'ALP'
      },
      {
          label: "银行卡支付",
          value: 'Bank'
      },
      {
          label: "现金支付",
          value: 'Cash'
      },
      {
          label: "银联支付",
          value: 'UPSMZF'
      },
      {
          label: "QQ支付",
          value: 'QQZF'
      },
      {
          label: "会员卡支付",
          value: 'Member'
      },
      {
          label: "余额支付",
          value: 'Account'
      },
      {
          label: "优惠支付",
          value: 'Coupon'
      },
    ],
    //支付标记
    tradeTypes: [
      {
        label: "扫码支付",
        value: 'NATIVE'
      },
      {
        label: "条码支付",
        value: 'BARCODE'
      },
      {
        label: "JSAPI",
        value: 'JSAPI'
      },
    ],
    //操作系统
    osTypes: [
      {
        label: "WIN PC",
        value: 'Windows'
      },
      {
        label: "IOS",
        value: 'IOS'
      },
      {
        label: "安卓",
        value: 'Android'
      },
      {
        label: "Window Phone",
        value: 'WP'
      },
      {
        label: "其他",
        value: 'OTHER'
      },
    ],
    //支付场景
    payScenes: [
      {
        label: "普通收银",
        value: 'CASHIER'
      },
      {
        label: "单柜收银",
        value: 'COUNTER_CASHIER'
      },
      {
        label: "员工收银",
        value: 'EMPLOYER_CASHIER'
      },
      {
        label: "线上商城",
        value: 'MALL_CASHIER'
      },
      {
        label: "台卡收款",
        value: 'TAB_CARD_CASHIER'
      },
      {
        label: "会员卡充值",
        value: 'MEMBER_RECHARGE'
      },
      {
        label: "代付金充值",
        value: 'PAID_GOLD_RECHARGE'
      },
      {
        label: "短信充值",
        value: 'SMS_RECHARGE'
      },
      {
        label: "直播订单",
        value: 'LIVE_ORDER'
      },
      {
        label: "软文订单",
        value: 'ADVERTORIAL_ORDER'
      },
      {
        label: "扫码点单",
        value: 'SCANQRCODE_CASHIER'
      },
      {
        label: "KTV预定",
        value: 'KTV_YD'
      },
      {
        label: "秒杀订单",
        value: 'SECKILL_ORDER'
      },
      {
        label: "拼团订单",
        value: 'GROUP_PURCHASE_ORDER'
      },
    ],
    // 订单类目
    orderTypes: [
      {
        label: "普通买单订单",
        value: 'GENERAL_ORDER'
      },
      {
        label: "商城订单",
        value: 'MALL_ORDER'
      },
      {
        label: "充值订单",
        value: 'RECHAR_ORDER'
      },
      {
        label: "团购订单",
        value: 'GROUP_PURCHASE_ORDER'
      },
      {
        label: "预售订单",
        value: 'WECHAT_PUB'
      },
      {
        label: "餐饮订单",
        value: 'REPAST_ORDER'
      },
      {
        label: "商超订单",
        value: 'SUPER_MARKET_ORDER'
      },
    ],
    // 支付状态
    payStatus: [
      {
        label: "付款成功",
        value: '6'
      },
      {
        label: "待支付",
        value: '1'
      },
      {
        label: "支付中",
        value: '2'
      },
      {
        label: "支付取消",
        value: '3'
      },
      {
        label: "支付失败",
        value: '4'
      },
      {
        label: "部分付款成功",
        value: '5'
      },
      {
        label: "退款中",
        value: '10'
      },
      {
        label: "已退款",
        value: '20'
      },
      {
        label: "退款失败",
        value: '30'
      },
    ],
    // 订单状态
    orderStatus: [
      {
        value: 1,
        label: "待付款"
      },
      {
        value: 2,
        label: "待发货"
      },
      {
        value: 3,
        label: "待用户收货"
      },
      {
        value: 4,
        label: "待用户自提"
      },
      {
        value: 10,
        label: "交易成功"
      },
      {
        value: 20,
        label: "交易关闭"
      },
      {
        value: 40,
        label: "已完成"
      },
    ]
  },
  effects: {
    *fetchNotices(_, { call, put, select }) {
      const data = yield call(queryNotices);
      yield put({
        type: 'saveNotices',
        payload: data,
      });
      const unreadCount = yield select(
        state => state.global.notices.filter(item => !item.read).length,
      );
      yield put({
        type: 'user/changeNotifyCount',
        payload: {
          totalCount: data.length,
          unreadCount,
        },
      });
    },

    *saveMenus({ payload }, {call, put}) {
      yield put({
        type: 'saveData',
        payload,
      });
    },

    *clearNotices({ payload }, { put, select }) {
      yield put({
        type: 'saveClearedNotices',
        payload,
      });
      const count = yield select(state => state.global.notices.length);
      const unreadCount = yield select(
        state => state.global.notices.filter(item => !item.read).length,
      );
      yield put({
        type: 'user/changeNotifyCount',
        payload: {
          totalCount: count,
          unreadCount,
        },
      });
    },

    *changeNoticeReadState({ payload }, { put, select }) {
      const notices = yield select(state =>
        state.global.notices.map(item => {
          const notice = { ...item };

          if (notice.id === payload) {
            notice.read = true;
          }

          return notice;
        }),
      );
      yield put({
        type: 'saveNotices',
        payload: notices,
      });
      yield put({
        type: 'user/changeNotifyCount',
        payload: {
          totalCount: notices.length,
          unreadCount: notices.filter(item => !item.read).length,
        },
      });
    },
  },
  reducers: {
    saveData(state, { payload }) {
      return {
        ...state,
        ...payload
      }
    },
    changeLayoutCollapsed(
      state = {
        notices: [],
        collapsed: true,
      },
      { payload },
    ) {
      return { ...state, collapsed: payload };
    },

    saveNotices(state, { payload }) {
      return {
        collapsed: false,
        ...state,
        notices: payload,
      };
    },

    saveClearedNotices(
      state = {
        notices: [],
        collapsed: true,
      },
      { payload },
    ) {
      return {
        collapsed: false,
        ...state,
        notices: state.notices.filter(item => item.type !== payload),
      };
    },
  },
  subscriptions: {
    setup({ history }) {
      // Subscribe history(url) change, trigger `load` action if pathname is `/`
      history.listen(({ pathname, search }) => {
        if (!JSON.parse(localStorage.getItem('hasSearchBtn')) && pathname != 'user/login') {
          // console.log(pathname);
          // router.push('/403')
        }
      });
    },
  },
};
export default GlobalModel;
